package com.isesol.ime.rest.api;

import com.github.pagehelper.*;
import com.isesol.api.rest.annoations.*;
import com.isesol.api.rest.model.base.*;
import com.isesol.arch.common.model.*;
import com.isesol.ime.Constant;
import com.isesol.ime.dal.model.*;
import com.isesol.ime.dal.model.dto.*;
import com.isesol.ime.dto.*;
import com.isesol.ime.rest.model.param.*;
import com.isesol.ime.service.*;
import org.apache.commons.lang3.*;
import org.springframework.stereotype.*;
import org.springframework.transaction.annotation.*;

import javax.annotation.*;
import java.util.*;

/**
 * 报文管理
 * <p>
 * <p>
 * <li> 新增报文
 * <li> 删除报文
 * <li> 修改报文
 * <li> 报文可选参数
 * <li> 报文已选参数
 * </p>
 *
 * @author peter zhang
 */
@Service
@RestServiceClass(url = "message")
public class MessageResource extends BusinessResources {

    @Resource
    MessageService messageService;

    @Transactional
    @RestServiceMethod(url = "create", requestType = MachineToolMessageRequest.class)
    public void create(MachineToolMessageRequest request) throws Exception {

        LOGGER.info(Constant.MODEL_NAME_MESSAGE + "create接口调用,request={}", request);

        MessageDTO messageDTO = modelMapper.map(request, MessageDTO.class);

        messageService.create(messageDTO);

    }

    @Transactional
    @RestServiceMethod(url = "del")
    public void del(@RestParam(name = "id") String id) throws Exception {

        LOGGER.info(Constant.MODEL_NAME_MESSAGE + "del接口调用,id={}", id);

        messageService.del(id);

    }

    @Transactional
    @RestServiceMethod(url = "update", requestType = MachineToolMessageRequest.class)
    public void update(MachineToolMessageRequest request) throws Exception {

        LOGGER.info(Constant.MODEL_NAME_MESSAGE + "update接口调用,request={}", request);

        MessageDTO messageDTO = modelMapper.map(request, MessageDTO.class);

        messageService.update(messageDTO);

    }

    @Transactional
    @RestServiceMethod(url = "find", requestType = MachineToolMessageRequest.class)
    public PageInfo find(MachineToolMessageRequest request) throws Exception {

        LOGGER.info(Constant.MODEL_NAME_MESSAGE + "find接口调用,request={}", request);

        MessageDTO messageDTO = modelMapper.map(request, MessageDTO.class);

        PageInfo pageInfo = messageService.find(messageDTO,
                request.getRestRequest().getPage(),
                request.getRestRequest().getRows());

        return pageInfo;

    }

    @Transactional
    @RestServiceMethod(url = "findById")
    public com.isesol.ime.dal.model.dto.MessageDTO findById(@RestParam(name = "id") String id) throws Exception {

        LOGGER.info(Constant.MODEL_NAME_MESSAGE + "findById接口调用,id={}", id);

        com.isesol.ime.dal.model.dto.MessageDTO msgDto = messageService.findById(id);

        if (msgDto.getIsDefault().equals("0")) {

            msgDto.setIsDefault("false");

        } else if (msgDto.getIsDefault().equals("1")) {

            msgDto.setIsDefault("true");
        }

        return msgDto;
    }

    @Transactional
    @RestServiceMethod(url = "getSubs")
    public List<SubscriptionDO> getSubs(@RestParam(name = "id") long id) throws Exception {

        LOGGER.info(Constant.MODEL_NAME_MESSAGE + "getSubs接口调用,id={}", id);

        return messageService.getSubs(id);

    }

    @Transactional
    @RestServiceMethod(url = "getSelectableParams", requestType = MachineToolMessageParamsRequest.class)
    public PageInfo getParams(MachineToolMessageParamsRequest request) throws Exception {

        LOGGER.info(Constant.MODEL_NAME_MESSAGE + "getParams接口调用,request={}", request);

        MachineToolMessageParamsDTO dto = modelMapper.map(request, MachineToolMessageParamsDTO.class);

        // todo 日后支持不同机床类型
        if (StringUtils.isBlank(dto.getParamVersion())) {
            dto.setParamVersion("1");
        }
        PageInfo pageInfo = messageService.findSelectableParams(dto,
                request.getRestRequest().getPage(),
                request.getRestRequest().getRows());

        return pageInfo;
    }


    @Transactional
    @RestServiceMethod(url = "getSelectedParams", requestType = MachineToolMessageRequest.class)
    public List<ParamDTO> getSelectedParams(MachineToolMessageRequest request) throws Exception {

        LOGGER.info(Constant.MODEL_NAME_MESSAGE + "getSelectedParams,request={}", request);

        MachineToolMessageDTO messageDTO = modelMapper.map(request, MachineToolMessageDTO.class);

        return messageService.getSelectedParams(messageDTO.getId());
    }

    @Transactional
    @RestServiceMethod(url = "copy", requestType = MachineToolMessageCopyRequest.class)
    public void copy(MachineToolMessageCopyRequest request) throws Exception {

        LOGGER.info(Constant.MODEL_NAME_MESSAGE + "copy接口调用,request={}", request);

        MessageDTO machineToolMessageCopyDTO = modelMapper.map(request, MessageDTO.class);

        messageService.messageCopy(machineToolMessageCopyDTO);

    }

}